﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
using System.Web;

namespace OnlineMediaAnalyzer.Models
{
    public class OnlineMediaAnalyzerContext : DbContext
    {
        // You can add custom code to this file. Changes will not be overwritten.
        // 
        // If you want Entity Framework to drop and regenerate your database
        // automatically whenever you change your model schema, please use data migrations.
        // For more information refer to the documentation:
        // http://msdn.microsoft.com/en-us/data/jj591621.aspx
    
        public OnlineMediaAnalyzerContext() : base("name=OnlineMediaAnalyzerContext")
        {
        }

        public DbSet<OnlineMediaAnalyzer.Models.Channel> Channels { get; set; }
        public DbSet<OnlineMediaAnalyzer.Models.Feed> Feeds { get; set; }
        public DbSet<OnlineMediaAnalyzer.Models.Article> Articles { get; set; }
        public DbSet<OnlineMediaAnalyzer.Models.RSSMap> RSSMaps { get; set; }
        public DbSet<OnlineMediaAnalyzer.Models.Category> Categories { get; set; }

        //Analyzer tables

        public DbSet<OnlineMediaAnalyzer.Models.FeedParty> FeedParties { get; set; }
        public DbSet<OnlineMediaAnalyzer.Models.FeedStatistic> FeedStatistics { get; set; }
        public DbSet<OnlineMediaAnalyzer.Models.MemberMention> MemberMentions { get; set; }
        public DbSet<OnlineMediaAnalyzer.Models.Party> Parties { get; set; }
        public DbSet<OnlineMediaAnalyzer.Models.PartyKeyword> PartyKeywords { get; set; }
        public DbSet<OnlineMediaAnalyzer.Models.PartyMember> PartyMembers { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

            modelBuilder.Entity<Article>()
                .HasMany(t => t.Categories)
                .WithMany(t => t.Articles)
                .Map(m =>
                {
                    m.ToTable("ArticleCategories");
                    m.MapLeftKey("ArticleID");
                    m.MapRightKey("CategoryID");
                });
            base.OnModelCreating(modelBuilder);
        }

        public override int SaveChanges()
        {
            try
            {
                return base.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Class: {0}, Property: {1}, Error: {2}",
                            validationErrors.Entry.Entity.GetType().FullName,
                            validationError.PropertyName,
                            validationError.ErrorMessage);
                    }
                }
                throw;
            }
        }

    }
}
